/**
 * 
 */
package com.blog.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.blog.pojo.SysRole;
import com.blog.pojo.SysUser;
import com.blog.service.SysRoleService;
import com.blog.service.SysUserRoleService;
import com.blog.service.SysUserService;

/**
 * @author zyj
 *
 */
@Controller
@RequestMapping("config")
public class UserController {
	@Autowired 
	private SysUserRoleService userRoleService;
    @Autowired 
    private SysUserService userService;
    @Autowired 
    private SysRoleService roleService;
    
    @RequestMapping("listUser")
    public String list(Model model) {
		List<SysUser> us = userService.list();
		model.addAttribute("us",us);
		Map<SysUser,List<SysRole>> user_roles = new HashMap<SysUser,List<SysRole>>();
		for (SysUser user : us) {
			List<SysRole> roles = roleService.listSysRoles(user);
			user_roles.put(user, roles);
		}
		model.addAttribute("user_roles", user_roles);
		return "listUser";
	}
    
    @RequestMapping("editUser")
    public String edit(Model model,long id) {
    	List<SysRole> rs = roleService.list();
    	model.addAttribute("rs",rs);
    	SysUser user = userService.get(id);
    	model.addAttribute("user",user);
    	List<SysRole> roles =roleService.listSysRoles(user);
        model.addAttribute("currentRoles", roles);
         
        return "editUser";
    }
    
    @RequestMapping("deleteUser")
    public String delete(Model model,long id){
        userService.delete(id);
        return "redirect:listUser";
    }
    
    @RequestMapping("updateUser")
    public String update(SysUser user,long[] roleIds){
        userRoleService.setRoles(user,roleIds);
         
        String password=user.getPassword();
        //������޸ĵ�ʱ��û���������룬�ͱ�ʾ���Ķ�����
        if(user.getPassword().length()!=0) {
            String salt = new SecureRandomNumberGenerator().nextBytes().toString();
            int times = 2;
            String algorithmName = "md5";
            String encodedPassword = new SimpleHash(algorithmName,password,salt,times).toString();
            user.setSalt(salt);
            user.setPassword(encodedPassword);
        }
        else
            user.setPassword(null);
         
        userService.update(user);
 
        return "redirect:listUser";
    }
    
    @RequestMapping("addUser")
    public String add(Model model,String name, String password){
         
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
        int times = 2;
        String algorithmName = "md5";
        String encodedPassword = new SimpleHash(algorithmName,password,salt,times).toString();
        SysUser u = new SysUser();
        u.setName(name);
        u.setPassword(encodedPassword);
        u.setSalt(salt);
        userService.add(u);
        return "redirect:listUser";
    }
    
    
}
